home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Development Platforms / Apple II / Apple II Sample Code / APW.SC / SC17Darts / Darts.c / UEvent.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-24  |  7.2 KB  |  249 lines  |  [TEXT/pdos]

  1. /***********************************************************************
  2. *
  3. * darts uevent.c -- Version 3.0 
  4. *
  5. * Copyright (c)
  6. * Steven E. Glass and Apple Computer, Inc.  1986-1990
  7. * All Rights Reserved.
  8. *
  9. * Developer Technical Support Apple II Sample Code
  10. *
  11. * This file contains the code which implements the 
  12. * main event loop used by the program.
  13. *
  14. ***********************************************************************/
  15.  
  16. #include <types.h>
  17. #include <locator.h>
  18. #include <quickdraw.h>
  19. #include <event.h>
  20. #include <intmath.h>
  21. #include <menu.h>
  22. #include <window.h>
  23. #include "darts.h"
  24.  
  25. extern WmTaskRec        event;
  26. extern GrafPortPtr      theWindow;
  27. extern unsigned int     quitFlag, gameType, weHaveAWinner, firstUpdateComplete;
  28. extern unsigned int     crickettTables[NumPlayers][26], score[NumPlayers];
  29.  
  30. static GrafPortPtr  lastWindow;         /* This private global is used in checkFrontW()
  31.                                         ** to prevent extra work when the windows
  32.                                         ** have not changed.  It is initialized
  33.                                         ** at the beginning of mainEvent().
  34.                                         */
  35.  
  36. extern char     test[80];
  37.  
  38.  
  39.  
  40. /************************************************************************************
  41. *
  42. * doScore
  43. *
  44. * This procedure does all the scoring in this program.  It is only called when
  45. * the user presses a button that has a non-zero low word for an ID.
  46. *
  47. * How the scoring works depends on the game being played.  There is a section
  48. * for scoring each of the games.
  49. *
  50. ************************************************************************************/
  51. void            doScore(playerNum, amount)
  52. unsigned int    playerNum, amount;
  53. {
  54.     unsigned int    otherPlayer, playerHits, otherHits, i, j;
  55.  
  56.     if (!gameType) {
  57.         score[playerNum] += amount;
  58.         addToList(playerNum, amount);
  59.         invalScore(playerNum);
  60.     }
  61.     else {
  62.         if (playerNum == (otherPlayer = Player1)) otherPlayer = Player2;
  63.         playerHits = crickettTables[playerNum][amount];
  64.         otherHits  = crickettTables[otherPlayer][amount];
  65.  
  66.  
  67.         if ((playerHits < 3) || (otherHits < 3)) {      /* At least one player is open. */
  68.             crickettTables[playerNum][amount] = ++playerHits;
  69.             fixButtonTitle(playerNum, amount);
  70.             if ((playerHits > 3) && (otherHits < 3)) {
  71.                 score[playerNum] += amount;
  72.                 invalScore(playerNum);
  73.             }
  74.             addToList(playerNum, amount);
  75.         }
  76.  
  77.         if (!weHaveAWinner) {
  78.             for (i = 0; i < 7; i++) {
  79.                 j = i + 15;
  80.                 if (i == 6) j = 25;
  81.                 if (crickettTables[playerNum][j] < 3) break;
  82.             }
  83.             if (
  84.                 (i == 7) &&         /* i is 7 when all are closed for this player. */
  85.                 (score[playerNum] >= score[otherPlayer])
  86.             ) {
  87.                 weHaveAWinner++;
  88.                 BeginUpdate(theWindow);
  89.                 drawThisWindow();
  90.                 EndUpdate(theWindow);
  91.                 AlertWindow(refIsResource * 2, NULL, 0x0003L);
  92.             }
  93.         }
  94.     }
  95. }
  96.  
  97.  
  98.  
  99. /************************************************************************************
  100. *
  101. * doControls
  102. *
  103. * This procedure is called by mainEvent whenever taskmaster returns inControl.
  104. *
  105. ***********************************************************************************/
  106. void    doControls()
  107. {
  108.     unsigned int    whichButton, buttonNum, lastItem, playerNum;
  109.  
  110.     if (whichButton = event.wmTaskData4) {      /* If the low word is not 0,
  111.                                                 ** then it is a scoring control.
  112.                                                 */
  113.         playerNum = Player1;
  114.         if (whichButton & 0x8000) playerNum = Player2;
  115.         whichButton &= 0x7FFF;
  116.         doScore(playerNum, whichButton);
  117.     }
  118.     else {
  119.         whichButton = HiWord(event.wmTaskData4);
  120.         switch(whichButton) {
  121.             case 1:
  122.                 buttonNum = AlertWindow(refIsResource * 2, NULL, ConfirmTextID);
  123.                 if (buttonNum == 1) doQuitItem();
  124.                 break;
  125.             case 2:
  126.                 doNewItem();
  127.                 break;
  128.             case 3:
  129.                 if (event.wmClickCount > 1) {
  130.                     removeSelected(Player1);
  131.                     event.wmClickCount = 0;
  132.                 }
  133.                 break;
  134.             case 4:
  135.                 if (event.wmClickCount > 1) {
  136.                     removeSelected(Player2);
  137.                     event.wmClickCount = 0;
  138.                 }
  139.                 break;
  140.         }
  141.     }
  142. }
  143.  
  144.  
  145.  
  146. void    DisableItems()
  147. {
  148.     DisableMItem(NewItem);
  149.     DisableMItem(QuitItem);
  150.     DisableMItem(AboutItem);
  151. }
  152.  
  153. void    EnableItems()
  154. {
  155.     EnableMItem(NewItem);
  156.     EnableMItem(QuitItem);
  157.     EnableMItem(AboutItem);
  158. }
  159.  
  160.  
  161.  
  162. /************************************************************************************
  163. *
  164. * checkFrontW
  165. *
  166. * This routine adjusts the system menu bar depending on what window is selected.
  167. * It activates the edit menu if a desk accessory is opened and selected.  It
  168. * deactivates the edit menu if the game window becomes active again.
  169. *
  170. * We do this because it makes no sense to have progam menus active when a desk
  171. * accessory is active and there is no sense having the edit menu active when
  172. * this program does not use it.
  173. *
  174. * It is called every time through the event loop.  The global variable lastWindow
  175. * helps us from making lots of checks if nothing has changed.
  176. *
  177. ************************************************************************************/
  178. void    checkFrontW()
  179. {
  180.     GrafPortPtr     theWindow;
  181.  
  182.     if ((theWindow = FrontWindow()) == lastWindow) return;
  183.  
  184.     if (!theWindow) {
  185.         SetMenuFlag(0x0080, EditMenuID);
  186.         DrawMenuBar();
  187.         DisableItems();
  188.     }
  189.     else {
  190.         if (GetSysWFlag(theWindow)) {
  191.             DisableItems();
  192.             SetMenuFlag(0xFF7F, EditMenuID);
  193.             SetMenuFlag(0x0080, GameMenuID);
  194.             DrawMenuBar();
  195.         }
  196.         else {
  197.             SetMenuFlag(0x0080, EditMenuID);
  198.             SetMenuFlag(0xFF7F, GameMenuID);
  199.             DrawMenuBar();
  200.             EnableItems();
  201.         }
  202.     }
  203.     lastWindow = theWindow;
  204. }
  205.  
  206.  
  207.  
  208. /************************************************************************************
  209. *
  210. * mainEvent
  211. *
  212. * This is the main routine in the program.  It continuously calls taskmaster and
  213. * handles any events that occur.  It also keeps the right menus active by 
  214. * calling checkFrontW.
  215. *
  216. ************************************************************************************/
  217. void    mainEvent()
  218. {
  219.     unsigned int    code, checkFirstUpdate;
  220.  
  221.     event.wmTaskMask = 0x001FFFFF;
  222.     quitFlag = 0;
  223.     checkFirstUpdate = 1;
  224.     lastWindow = NULL;
  225.  
  226.     for (;;) {
  227.         checkFrontW();
  228.         code = TaskMaster(0xFFFF, &event);
  229.         switch (code) {
  230.             case wInSpecial:
  231.             case wInMenuBar:
  232.                 doMenu();
  233.                 break;
  234.             case wInControl:
  235.                 doControls();
  236.                 break;
  237.             default:
  238.                 if (checkFirstUpdate) {
  239.                     if (firstUpdateComplete) {
  240.                         InitCursor();
  241.                         checkFirstUpdate = 0;
  242.                     }
  243.                 }
  244.                 break;
  245.         }
  246.         if (quitFlag) break;
  247.     }
  248. }
  249.